package org.loofer.uiframework;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Bundle;
import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.SdkVersionUtils;
import org.loofer.uiframework.navigator.backstack.FragmentBackStackManager;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.spi.Configurator;

public class ActivityManager {
    private static final String TAG = "ActivityManager";
    private static ActivityManager sInstance;
    private List<Activity> mActivities = new ArrayList();
    private Activity mCurrentActivity = null;
    private Activity mLastActivity = null;
    private Activity mSavedLastActivity = null;

    public static ActivityManager instance() {
        if (sInstance == null) {
            sInstance = new ActivityManager();
        }
        return sInstance;
    }

    public void onCreate(Activity activity) {
        this.mActivities.add(activity);
    }

    public void onDestroy(Activity activity) {
        if (this.mLastActivity == activity) {
            this.mLastActivity = null;
        }
        if (this.mSavedLastActivity == activity) {
            this.mSavedLastActivity = null;
        }
        this.mActivities.remove(activity);
    }

    @TargetApi(16)
    public void finishActivities() {
        Logger.d(TAG, "will exit app, finishActivities");
        for (Activity activity : this.mActivities) {
            if (SdkVersionUtils.hasJellyBean()) {
                activity.setResult(0);
                activity.finishAffinity();
            }
            activity.finish();
        }
        this.mActivities.clear();
    }

    public void onResume(Activity activity) {
        this.mCurrentActivity = activity;
        if (this.mLastActivity == activity) {
            this.mLastActivity = this.mSavedLastActivity;
        }
    }

    public void onPause(Activity activity) {
        this.mSavedLastActivity = this.mLastActivity;
        this.mLastActivity = activity;
    }

    public Activity getCurrentActivity() {
        return this.mCurrentActivity;
    }

    public Activity getLastActivity() {
        return this.mLastActivity;
    }

    public List<Activity> getActivities() {
        return this.mActivities;
    }

    public boolean isCurrentActivity(Activity activity) {
        return activity != null && activity == getCurrentActivity();
    }

    public String dumpLastActivityInfo() {
        return getActivityInfo(this.mCurrentActivity);
    }

    private String getActivityInfo(Activity activity) {
        if (activity == null) {
            return "null activity has no info";
        }
        StringBuilder builder = new StringBuilder("");
        builder.append(activity.getClass().getSimpleName());
        builder.append(":{");
        try {
            builder.append(", mToken").append(getField(activity, "mToken"));
            builder.append(", isFinished=").append(activity.isFinishing());
            builder.append(", action=").append(activity.getIntent());
            builder.append(", mWindow=").append(activity.getWindow().toString());
            builder.append(", mWindowManager=").append(activity.getWindowManager());
            builder.append(", mCallingActivity=").append(activity.getCallingActivity());
            builder.append(", mWindowAdded=").append(getField(activity, "mWindowAdded"));
            builder.append(", mVisibleFromClient=").append(getField(activity, "mVisibleFromClient"));
            builder.append(", mActivityInfo=").append(getField(activity, "mActivityInfo"));
            Bundle bundle = activity.getIntent().getExtras();
            if (bundle != null) {
                builder.append(", extra=");
                for (String key : bundle.keySet()) {
                    builder.append(key).append(",").append(bundle.get(key)).append("&");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (activity instanceof BaseActivity) {
            FragmentBackStackManager manager = ((BaseActivity) activity).getFragmentBackStackManager();
            if (!(manager == null || manager.getTopFragment() == null)) {
                builder.append(", topFragment=").append(manager.getTopFragment());
            }
        }
        builder.append("}\r\n");
        return builder.toString();
    }

    private String getField(Activity activity, String field) throws Exception {
        Field f = Activity.class.getDeclaredField(field);
        f.setAccessible(true);
        Object token = f.get(activity);
        if (token != null) {
            return token.toString();
        }
        return Configurator.NULL;
    }
}
